package com.mediastory.spsserver.api.dao.impl;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.mediastory.spsserver.api.dao.ApiDao;
import com.mediastory.spsserver.api.domain.Info_v1_0;
import com.mediastory.spsserver.api.domain.Notice_v1_0;
import com.mediastory.spsserver.api.domain.Content_v1_0;
import com.mediastory.spsserver.api.domain.UrgencyNotice_v1_0;

@Repository
@Component("ApiDaoImpl_v1_0")
public class ApiDaoImpl_v1_0 implements ApiDao {

	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	public ApiDaoImpl_v1_0() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<Object> SelectListCategory(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		List<Object> result = sqlSessionTemplate.selectList("Api.selectList.category", condition);
		return result;
	}

	@Override
	public List<Object> SelectListContent(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		Integer page = (Integer)condition.get("page");
		Integer maxnum = (Integer)condition.get("maxnum");
		
		List<Object> result = null;
		if(page != null && maxnum != null){
			RowBounds rb = new RowBounds(page, maxnum);
			result = sqlSessionTemplate.selectList("Api.selectList.content_v1_0", condition, rb);
		}else{
			result = sqlSessionTemplate.selectList("Api.selectList.content_v1_0", condition);
		}

		return result;
	}
	
	@Override
	public List<Content_v1_0> SelectListContent2(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		Integer page = (Integer)condition.get("page");
		Integer maxnum = (Integer)condition.get("maxnum");
		
		List<Content_v1_0> result = null;
		if(page != null && maxnum != null){
			RowBounds rb = new RowBounds(page, maxnum);
			result = sqlSessionTemplate.selectList("Api.selectList.content_v1_0", condition, rb);
		}else{
			result = sqlSessionTemplate.selectList("Api.selectList.content_v1_0", condition);
		}

		return result;
	}

	@Override
	public Integer SelectListContTotalCnt(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		Integer result = sqlSessionTemplate.selectOne("Api.selectListTotalCnt.cont_v1_0", condition);
		return result;
	}
	
	@Override
	public List<Object> SelectListMainCont(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		Integer page = (Integer)condition.get("page");
		Integer maxnum = (Integer)condition.get("maxnum");
		
		List<Object> result = null;
		if(page != null && maxnum != null){
			RowBounds rb = new RowBounds(page, maxnum);
			result = sqlSessionTemplate.selectList("Api.selectList.mainCont_v1_0", condition, rb);
		}else{
			result = sqlSessionTemplate.selectList("Api.selectList.mainCont_v1_0", condition);
		}

		return result;
	}
	
	
	@Override
	public List<String> SelectListImgUrl(Map<String, Object> condition) throws Exception {
		List<String> result = sqlSessionTemplate.selectList("Api.selectList.imgUrl", condition);
		return result;
	}
	
	
	@Override
	public Integer SelectListNoticeTotalCnt(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		Integer result = sqlSessionTemplate.selectOne("Api.selectListTotalCnt.notice_v1_0", condition);
		return result;
	}

	@Override
	public Notice_v1_0 SelectNotice(Map<String, Object> condition) throws Exception {
		Notice_v1_0 result = sqlSessionTemplate.selectOne("Api.select.notice_v1_0", condition);
		return result;	
	}
	
	@Override
	public UrgencyNotice_v1_0 SelectUrgencyNotice(Map<String, Object> condition) throws Exception {
		UrgencyNotice_v1_0 result = sqlSessionTemplate.selectOne("Api.select.urgencyNotice_v1_0", condition);
		return result;	
	}
	
	@Override
	public List<Object> SelectNoticeList(Map<String, Object> condition) throws Exception {
		List<Object> result = sqlSessionTemplate.selectList("Api.select.noticeList_v1_0", condition);
		return result;	
	}

	@Override
	public Info_v1_0 SelectInfo(Map<String, Object> condition) throws Exception {
		Info_v1_0 result = sqlSessionTemplate.selectOne("Api.select.info_v1_0", condition);
		return result;
	}

	@Override
	public Map<String, Object> SelectVerMngr(Map<String, Object> condition) throws Exception {
		Map<String, Object> result = sqlSessionTemplate.selectOne("Api.select.verMngr_v1_0", condition);
		return result;
	}
	
	@Override
	public Map<String, Object> SelectVerMngr2(Map<String, Object> condition) throws Exception {
		Map<String, Object> result = sqlSessionTemplate.selectOne("Api.select.verMngr2_v1_0", condition);
		return result;
	}

	@Override
	public List<Object> SelectListAdvertising(Map<String, Object> condition) throws Exception {
		List<Object> result = sqlSessionTemplate.selectList("Api.selectList.advertising_v1_0", condition);
		return result;
	}
	
	@Override
	public List<Object> SelectListPayContAndriod(Map<String, Object> condition) throws Exception {
		List<Object> result = sqlSessionTemplate.selectList("Api.selectList.payContAndroid_v1_0", condition);
		return result;
	}
	
	@Override
	public List<Object> SelectListPayContIos(Map<String, Object> condition) throws Exception {
		List<Object> result = sqlSessionTemplate.selectList("Api.selectList.payContIos_v1_0", condition);
		return result;
	}
	
	@Override
	public List<Object> SelectListGuide(Map<String, Object> condition) throws Exception {
		List<Object> result = sqlSessionTemplate.selectList("Api.selectList.guide_v1_0", condition);
		return result;
	}

	@Override
	public int InsertGcmDevice(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.GcmDevice_v1_0", condition);
	}

	@Override
	public int deleteGcmDevice(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.update("Api.delete.GcmDevice_v1_0", condition);
	}
	
	@Override
	public int InsertApnsDevice(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.ApnsDevice_v1_0", condition);
	}

	@Override
	public int deleteApnsDevice(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.update("Api.delete.ApnsDevice_v1_0", condition);
	}
	
	@Override
	public Map<String, Object> insertCont(Map<String, Object> contLog)
			throws Exception {
		// TODO Auto-generated method stub
		sqlSessionTemplate.insert("Api.insert.log.cont", contLog);
		return contLog;
	}

	@Override
	public Map<String, Object> insertPage(Map<String, Object> pageLog)
			throws Exception {
		// TODO Auto-generated method stub
		sqlSessionTemplate.insert("Api.insert.log.page", pageLog);
		return pageLog;
	}

	@Override
	public Map<String, Object> insertUnit(Map<String, Object> unitLog)
			throws Exception {
		// TODO Auto-generated method stub
		sqlSessionTemplate.insert("Api.insert.log.unit", unitLog);
		return unitLog;
	}
	
	@Override
	public Map<String, Object> insertDl(Map<String, Object> logDl)
			throws Exception {
		// TODO Auto-generated method stub
		sqlSessionTemplate.insert("Api.insert.log.dl", logDl);
		return logDl;
	}
	
	@Override
	public Integer insertLogInfo(Map<String, Object> logInfo)
			throws Exception {
		// TODO Auto-generated method stub
		sqlSessionTemplate.insert("Api.insert.log.logInfo", logInfo);
		return (Integer)logInfo.get("idx");
	}
	
	@Override
	public int insertAppLifeCycle(JSONObject appLifeCycle)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.appLifeCycle", appLifeCycle);
	}

	@Override
	public int insertDownCont(JSONObject downCont) throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.downCont", downCont);
	}

	@Override
	public int insertConfirmPush(JSONObject confirmPush)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.confirmPush", confirmPush);
	}

	@Override
	public int insertConfirmNoti(JSONObject confirmNoti)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.confirmNoti", confirmNoti);
	}

	@Override
	public int insertAppUpdate(JSONObject appUpdate) throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.appUpdate", appUpdate);
	}

	@Override
	public int insertSetupPush(JSONObject setupPush) throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.setupPush", setupPush);
	}

	@Override
	public int insertError(JSONObject error) throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.error", error);
	}

	@Override
	public int insertDetailedEvent(JSONObject detailedEvent)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.detailedEvent", detailedEvent);
	}

	@Override
	public int insertAdClick(JSONObject adClick)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.adClick", adClick);
	}

	@Override
	public int insertAdRolling(JSONObject adRolling)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.adRolling", adRolling);
	}

	@Override
	public int insertViewCont(JSONObject viewCont)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.viewCont", viewCont);
	}

	@Override
	public int insertDetailedCont(JSONObject detailedCont)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.detailedCont", detailedCont);
	}
	
	@Override
	public int insertView(JSONObject view)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.view", view);
	}
	
	@Override
	public int insertViewlog(JSONObject viewlog)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.viewlog", viewlog);
	}
	
	@Override
	public int insertViewlogData(JSONObject viewlogData)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.log.viewlogData", viewlogData);
	}
	
	@Override
	public Integer insertPurLogInfo(Map<String, Object> logInfo)
			throws Exception {
		// TODO Auto-generated method stub
		sqlSessionTemplate.insert("Api.insert.pur.purLogInfo", logInfo);
		return (Integer)logInfo.get("idx");
	}
	
	@Override
	public int insertIosPurchaseData(JSONObject purchaseData)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.pur.iosPurchaseData", purchaseData);
	}
	
	@Override
	public int insertAndroidPurchaseData(JSONObject purchaseData)
			throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.insert.pur.androidPurchaseData", purchaseData);
	}
	
	@Override
	public Integer SelectAppSeqId(Map<String, Object> appSeqId) throws Exception {
		// TODO Auto-generated method stub
		sqlSessionTemplate.insert("Api.insert.appSeqId", appSeqId);
		return (Integer)appSeqId.get("idx");
	}
	
	@Override
	public Integer insertContent(JSONObject data) throws Exception {
		// TODO Auto-generated method stub
		sqlSessionTemplate.insert("Api.insert.content", data);
		return (Integer)data.get("idx");
	}
	
	@Override
	public int updateContent(JSONObject data) throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.update.content", data);
	}
	
	@Override
	public int deleteContent(JSONObject data) throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.delete.content", data);
	}
	
	@Override
	public Integer contFileinsert(Map<String, Object> contFile)	throws Exception {
		// TODO Auto-generated method stub
		sqlSessionTemplate.insert("Api.insert.contFile", contFile);
		return (Integer)contFile.get("idx");
	}	
	
	@Override
	public Integer insertComment(JSONObject data) throws Exception {
		// TODO Auto-generated method stub
		sqlSessionTemplate.insert("Api.insert.comment", data);
		return (Integer)data.get("idx");
	}
	
	@Override
	public int deleteComment(JSONObject data) throws Exception {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.insert("Api.delete.comment", data);
	}
	
	@Override
	public List<Object> SelectListComment(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub

		List<Object> result = null;
		result = sqlSessionTemplate.selectList("Api.selectList.comment_v1_0", condition);

		return result;
	}
	
}